﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Castle.Core.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Text.RegularExpressions;
using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
using MvcContrib.Pagination;
using System.Data.Entity.Validation;

using CooWork.Models;
using CooWork.Services;
using Coowork.Data.Mapping;

namespace CooWork.Controllers
{
    public class TagController : BaseController
    {
        private CooWorkEntities context = new CooWorkEntities();
        //
        // GET: /Tag/

        public ActionResult Index()
        {
            return View();
        }


        [HttpPost]
        [TaskEditableAttribute]
        public JsonResult JCreate(string cname,int? taskId)
        {
            if (string.IsNullOrWhiteSpace(cname) || !taskId.HasValue)
            {
                return new JsonResult();
            }

            cname = Server.UrlDecode(cname);
            CooTask cootask = context.CooTasks.Single(x => x.ID == taskId);

            if (!string.IsNullOrWhiteSpace(cootask.tags) && cootask.tags.Contains(cname))
            {
                return new JsonResult { Data = new { flag = "error" } };
            }            
           
            CooTag ct = new CooTag();
            ct.Tagname = cname;
            ct.IsPublic = true;
            ct.cid = UserService.UcId();
            ct.tcount = 0;

            CooTag ct2 = context.CooTags.Where(x => x.Tagname == cname).Where(x => x.cid == ct.cid).SingleOrDefault();

            if (ct2 != null)
            {

                ct2.tcount = ct2.tcount + 1;
                context.Entry(ct2).State = EntityState.Modified;
                context.SaveChanges();
            }
            else
            {
                if (ModelState.IsValid)
                {                  
                    context.CooTags.Add(ct);
                    context.SaveChanges();                   
                }
                else
                {
                    return new JsonNetResult { Data = ModelState.Select(x => x.Value.Errors).Where(x => x.Count > 0).ToList() };
                }

            }
                  
          
            cootask.tags = (cootask.tags + "," + ct.Tagname.Replace(',', ' ')).Trim(',');
            if (ModelState.IsValid)
            {
                context.Entry(cootask).State = EntityState.Modified;
                context.SaveChanges();
            }
            else
            {
                return new JsonNetResult { Data = ModelState.Select(x => x.Value.Errors).Where(x => x.Count > 0).ToList() };
            }

            return new JsonNetResult { Data = new { flag = "ok" } };      
            
      
        }

        [HttpPost] 
        [TaskEditableAttribute]     
        public JsonResult JDel(string cname, int? taskId)
        {
            if (string.IsNullOrWhiteSpace(cname) || !taskId.HasValue)
            {
                return new JsonResult();
            }
            cname = Server.UrlDecode(cname);

            CooTask cootask = context.CooTasks.Single(x => x.ID == taskId);

            if (!string.IsNullOrWhiteSpace(cootask.tags) || cootask.tags.Contains(cname))
            {
                cootask.tags = Regex.Replace(cootask.tags, @",?"+cname, "", RegexOptions.IgnoreCase);
                cootask.tags = cootask.tags.Trim(',');
                if (ModelState.IsValid)
                {
                    context.Entry(cootask).State = EntityState.Modified;
                    context.SaveChanges();
                }
                else
                {
                    return new JsonNetResult { Data = ModelState.Select(x => x.Value.Errors).Where(x => x.Count > 0).ToList() };
                }

                return new JsonNetResult { Data = new { flag="ok" } };
            }

            return new JsonNetResult { Data = new { flag = "error" } };
          
        }



    }
}
